// pages/companyCenter/companyCenter.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    companyInfo: null,
    /**
       * tabBar
       */
    tabBar: null,
    status: null,
    reason: null,
  },
  /**
   * 改变logo
   */
  changeAvatar: function () {
    var that = this;

    if (app.globalData.userType == 1) {
      return;
    }

    wx.chooseImage({

      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths);

        wx.uploadFile({
          url: app.globalData.server + '/user/avatar/',
          filePath: tempFilePaths[0],
          method: 'POST',
          name: 'avatar',
          header: {
            'content-type': 'application/x-www-form-urlencoded', // 默认值
            'Authorization': 'Bearer ' + app.globalData.token,
            'x-wxapp-signature': null,
            'x-wxapp-timestamp': app.getTimestamp(),
            'x-wxapp-nonce': app.getRandom()
          },
          success: function (res) {
            //var data = res.data
            console.log(res);
            wx.redirectTo({
              url: '../init/init',
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    that.setData({
      companyInfo: app.globalData.userInfo,
      status: app.globalData.companyStatus,
      reason: app.globalData.reason,
      //status: '未通过',
      //status: '未审核',
    });
    if (that.data.status == "已通过" || that.data.status == "测试企业") {
      // 查询企业发起的悬赏检查当年的悬赏情况
      wx.request({
        url: app.globalData.server + '/predict/reward/',
        method: 'GET',
        data: {},
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
          'Authorization': 'Bearer ' + app.globalData.token,
          'x-wxapp-signature': null,
          'x-wxapp-timestamp': app.getTimestamp(),
          'x-wxapp-nonce': app.getRandom()
        },
        success: function (res) {
          console.log(res);
          var rewardList = res.data.data;
          if (rewardList == null) {
            rewardList = [];
          }
          for (var i = 0; i < rewardList.length; i++) {
            var reward = rewardList[i];
            console.log(reward);
            if (reward.item == app.globalData.thisYear) {
              console.log("find!!!")
              app.globalData.currentYearRewardInfo.reward_id = reward.id;
              app.globalData.currentYearRewardInfo.reward_amount = reward.reward;

              // 当年已经发起悬赏且支付
              if (reward.status != "unpaid") {
                app.globalData.currentYearRewardInfo.hasLaunched = true;
                //  当年已经发起悬赏未支付| 当年已经追加悬赏未支付 进入发起悬赏页面默认为支付当年
              } else {
                app.globalData.currentYearRewardInfo.hasLaunched = false;
                if (reward.added_score != 0) {
                  app.globalData.currentYearRewardInfo.addition = true;
                }
              }
              break;
            }
          }
          console.log(app.globalData.currentYearRewardInfo);
        }
      });
      // 还未通过审核的用户
    } else if (app.globalData.userType == 1) {
      wx.request({
        url: app.globalData.server + '/user/company/auth/',
        method: 'GET',
        data: {},
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
          'Authorization': 'Bearer ' + app.globalData.token,
          'x-wxapp-signature': null,
          'x-wxapp-timestamp': app.getTimestamp(),
          'x-wxapp-nonce': app.getRandom()
        },
        success: function (res) {
          console.log(res);
          app.globalData.userInfo = res.data.data;
          app.globalData.userInfo.company_logo = "https://doclinzh.com/media/default/default_avatar.png";
          that.setData({
            companyInfo: app.globalData.userInfo,
            status: app.globalData.companyStatus,
            reason: app.globalData.reason,
            'companyInfo.total_score': 0,
            'companyInfo.valid_score': 0,
            //status: '未通过',
            //status: '未审核',
          });
        }
      })

    }
    // tabBar
    app.editTabBar();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})